package cn.est.controller;

import cn.est.pojo.Users;
import cn.est.utils.RedisUtil;
import cn.est.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMethod;
import io.swagger.annotations.ApiImplicitParams;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;



//测试控制器
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    SMSUtil smsUtil;
    //测试redis缓存
    @RequestMapping(value="/setredis", method=RequestMethod.POST)
    public Object setRedis() {
        Users users=new Users();   //model层pojo实例
        users.setUserName("林辉煌");
        users.setAccount("xxx");//Account账户
        redisUtil.set("LHH2",users);//将key与value传入redis
        return "创建用户成功";
    }
    //测试redis缓存
    @RequestMapping(value="/getredis", method=RequestMethod.GET)
    public Object getRedis() {
    Users users=(Users)redisUtil.get("LHH2");//获取redis中key为LHH2的值
        return users;
    }
    //测试swagger模拟接口--—无参
//notes接口描述(注释),httpMethod(接口请求方式),response(接口返回参数类型),nickname(接口发布说明),protocols(协议)
    @ApiOperation(value="获取所有用户信息", notes="获取所有用户信息",httpMethod="GET")
    //required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”
   // @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="/allusers", method=RequestMethod.GET)
    public Object getAllUser() {
        return "所有用户信息";
    }

    //测试swagger模拟接口---有参(如果入参只有一个就在@ApiImplicitParams注解里放一个)
    @ApiOperation(value="根据用户id获取用户信息", notes="根据用户id获取用户信息",httpMethod="GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int")
//  ,@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @RequestMapping(value="/userbyid", method=RequestMethod.GET)
    public Object getUserById(@RequestParam(required = true,value = "id") Integer id) {
        return "获取uid="+id+"用户信息";
    }
//测试阿里短信服务——通过配置类读取配置文件方式
@RequestMapping(value="/sendsms", method= RequestMethod.GET)
public Object sentMsg(){
//SendSmsResponse对象导包import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
    SendSmsResponse response=null;
    try {
        response= smsUtil.sendSms("15994804883","{code:12345}");
        return "发送短信成功"+response.body.getCode();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "发送短信失败"+response.body.getCode();
}





}
